﻿/// <reference path="jquery-1.7.1-vsdoc.js" />
/// <reference path="MyLibrary.js" />
/// <reference path="MyAjax.js" />


$(document).ready(function () {

    $("table").width($("#theinput").width() + 6);
    var top = screen.availHeight / 2 - 200;
    var left = (screen.availWidth - $("#theinput").width()) / 2 - 50;

    $("div").offset({ top: top, left: left });

    $("table").hide();

    $("body").keyup(function (event) {
        var key = event.which;
        if (key == 13) {
            operateTheValue($("#theinput").val());
        }
    });

    $("body").click(function () {
        $("table").hide();
        $("table tbody").empty();
    });

    $("#send").click(function (event) {
        operateTheValue($("#theinput").val());
    });

    $("#theinput").keyup(function (event) {
        var key = event.which;
        if (key == 38 || key == 40 || key == 13)
            return;
        $("tbody").empty();
        var value = $(this).val();
        $("#theinput").data("value", value);
        value = trimStrSpace(value);
        if (value.length != 0) {
            //            $.get("Baidu.ashx", { input: value }, function (data, textStatus, jqXHR) {
            //                var i = 0;
            //                var result = Convert.jsonStringToObject(data);
            //                if (result.length != 0) $("table").show();
            //                for (x in result) {
            //                    i++;
            //                    var tr = $("<tr></tr>");
            //                    var td = $("<td></td>");
            //                    if (i > 10) break;
            //                    lastResult = result[x].replace(value, "<span>" + value + "</span>");
            //                    var b = "<b>" + lastResult + "</b>";
            //                    td.html(b);
            //                    tr.append(td);
            //                    $("tbody").append(tr);
            //                }
            //                $("tr").mousedown(function () {
            //                    var val = $(this).text();
            //                    $("#theinput").val(val);
            //                    operateTheValue(val);
            //                });

            //                $("tr").mouseover(function () {
            //                    $("tr").css("background-color", "transparent");
            //                    setBackground(this);
            //                });

            //                $("tr").mouseleave(function () {
            //                    $("tr").css("background-color", "transparent");
            //                });
            //            });
            var x = new A();
            var encodeValue = encodeURIComponent(value);

            x.setDefault("Baidu.ashx?" + "input=" + encodeValue);
            x.setHeader("Accept", "application/json, text/javascript, */*; q=0.01");
            x.setHeader("Content-Type", "application/json; charset=utf-8");
            //alert(object.getPP());
            //x.setOnReadyStateChange(x.stateChange);

            x.success = function (text, headers) {
                var i = 0;
                var result = Convert.jsonStringToObject(text);
                if (result.length != 0) $("table").show();
                for (x in result) {
                    i++;
                    var tr = $("<tr></tr>");
                    var td = $("<td></td>");
                    if (i > 10) break;
                    lastResult = result[x].replace(value, "<span>" + value + "</span>");
                    var b = "<b>" + lastResult + "</b>";
                    td.html(b);
                    tr.append(td);
                    $("tbody").append(tr);
                }
                $("tr").mousedown(function () {
                    var val = $(this).text();
                    $("#theinput").val(val);
                    operateTheValue(val);
                });

                $("tr").mouseover(function () {
                    $("tr").css("background-color", "transparent");
                    setBackground(this);
                });

                $("tr").mouseleave(function () {
                    $("tr").css("background-color", "transparent");
                });
            }
            var encodeValue = encodeURIComponent(value);
            //x.setData({ input: encodeValue });
            x.setData();

        }
        else {
            $("table").hide();
        }
    });

    $("div").keyup(function (event) {
        var key = event.which;
        if (key == 13)
            return;
        var selectIndex = -1;
        var isInit = false;
        var originalColor = "Green";
        var childLength = $("tbody").children().length;
        $("tbody").children().each(function (index, element) {
            var color = $(element).css("background-color");
            if (color == "Green" || color == "rgb(0, 128, 0)") {
                selectIndex = index;
                isInit = true;
            }
            else {
                originalColor = color;
            }
        });
        setBackground("tr:eq(" + selectIndex + ")", originalColor);
        if (key == 38) {
            selectIndex--;
        }
        else if (key == 40) {
            selectIndex++;
        }
        if (childLength != 0) {
            if (selectIndex == childLength || selectIndex == -1) {
                var value = $("#theinput").data("value")
                $("#theinput").val(value);
            } else if (selectIndex > childLength - 1) {
                selectIndex = 0;
            } else if (selectIndex < 0) {
                selectIndex = childLength - 1;
            }
        }


        //一个时候的异常。
        if (childLength == 1) {
            var css = $("tr").css("background-color");
            if (css == "Green" || css == "rgb(0, 128, 0)") {
                $("tr").css("background-color", "transparent");
            }
            else {
                setBackground("tr");
            }

        } else {
            setBackground("tr:eq(" + selectIndex + ")");
        }


        if ($("tr:eq(" + selectIndex + ")").length == 1) {
            $("#theinput").val($("tr:eq(" + selectIndex + ")").text());
        }

    });
});

function setBackground(object, color) {
    if (arguments.length == 2) {
        $(object).css("background-color", color);
    }
    else {
        $(object).css("background-color", "Green");
    }

}

function operateTheValue(str) {
    alert(str);
}
